#include <curses.h>

#define KEY_CTRL(k) ((k) & 0x1f)

static int x = 0;
static int y = 0;

static void
init()
{
  initscr();
  raw();    // raw mode
  noecho(); // switch to non-echo
  keypad(stdscr, TRUE); // enable function key(F1,Up/Down...)
}

int main()
{
  init();
  int c;
  int row, col;
  getmaxyx(stdscr, row, col);

  while ((c = getch()) != KEY_CTRL('q')) {
    if (c == KEY_CTRL('h') || c == KEY_LEFT) {
      x = x - 1 < 0 ? 0 : x - 1;
      move(y, x);
    } else if (c == KEY_CTRL('j') || c == KEY_DOWN) {
      y = y + 1 > col ? col : y + 1;
      move(y , x);
    } else if (c == KEY_CTRL('k') || c == KEY_UP) {
      y = y - 1 < 0 ? 0 : y - 1;
      move(y, x);
    } else if (c == KEY_CTRL('l') || c == KEY_RIGHT) {
      x = x + 1 > row ? row : x + 1;
      move(y, x);
    } else {
      printw("%c", c);
    }
    refresh();
  }

  endwin();

  return 0;
}
